
var modifyOrAddFlag;
//初始化数据

	
  $(document).ready(function(){
		$('#win').window('close');

		//导入
		$('#import_link').window({
			width:465,
			modal:true,
			minimizable:false,
			maximizable:false,
			collapsible:false
		});
		$('#import_link').window('close');  //关闭窗口
		
		$("#selFileBtn").click(function(e){
			e.preventDefault();
			//$("#file").click();
		});
		
		$(".import_con").delegate("#importLinkFile" ,"change", function() {
		    $("#fileName").val($('#importLinkFile').val());
		    //clearAutofillStyle();
		});
		
		$(".button_con a" ).removeClass("l-btn-plain");
		$("#datagrid").datagrid({
			url : BASE_URL_EXCEPTION_ERROR_CODE_SETTING_SEARCH + '/findByExample' + '?v=' + Math.random(),
			method:'get',
			autoRowHeight:true,
			nowrap:false,
			pageSize:20,
			singleSelect:true,
			striped:true,
			pageList:[10,20,50,100],
			columns:[[
						{field:'',width:10,checkbox:true},
						{field:'errorCode',title:'异常错误码',width:100},
						{field:'errorDesc',title:'异常错误码描述',width:300,align:'left'}
			      ]],
			pagination:true,
			onSelect:function(rowIndex,rowData){
				$("#toolbar_edit").linkbutton('enable');
				$("#toolbar_delete").linkbutton('enable');
			},
			loadFilter:function(data){
				return {
					total: data.totalElements,
					rows: data.content
				};
			},
			onBeforeLoad:function(param){
				param.page -= 1;
				param.size = param.rows;
				delete param.rows;
				param.errorCode = $("#searchCode").val();
				param.errorDesc = $("#searchDesc").val();
				
				param.sort = 'createTime,desc';
				
			}
		});
		
		$("#datagrid").datagrid('getPager').pagination({   
		    displayMsg:'当前显示从 [{from}] 到 [{to}] 共[{total}]条记录'
		}); 

});
 
 	function add(){
 		modifyOrAddFlag = true;
 		$('#addForm')[0].reset();
 		$("#win").window('setTitle','异常错误码设置-新增');
 		$('#win').window('open');
	}
 	//新增和修改
 	function save(){
 		if($("#addForm").form("validate")){
 	 		var errorCode = $("#txtErrorCode").val();
 	 		var errorDesc = $("#txtErrorCodeDesc").val();
 	 		var newFieldInfo = {errorCode:errorCode,errorDesc:errorDesc};
 	 		var checkParam = {errorCode:errorCode,errorDesc:errorDesc};
			$("#instance_save").linkbutton("disable");
			$("#instance_save .l-btn-text").text("提交中...");
	 		if(!modifyOrAddFlag){
	 	 		var row = $('#datagrid').datagrid('getSelected');
	 	 		checkParam.originalId = row.id;
	 	 		if(row.errorCode == checkParam.errorCode) // 未修改领域的名字，则可以直接修改
				{
	 	 			modifyErrorCode(newFieldInfo);
				}
				else
				{
					$.ajax({
			    		type: "get",
			    		url: BASE_URL_EXCEPTION_ERROR_CODE_SETTING_SEARCH + '/checkIsCanSave' + '?v=' + Math.random(),
			    		data: checkParam,
			    		contentType: "application/json",
			    		success: function(result) {
			    			if(result.isCanSave == true)
		    				{
			    				modifyErrorCode(newFieldInfo);
		    				}
		    				else 
	    					{
		    					if (result.errorCode == "DUPLICATE") 
								{
			    					$("#instance_save").linkbutton("enable");
			    					$("#instance_save .l-btn-text").text("保存");
									$.messager.alert('错误',"<p>修改失败，异常错误码已存在！</p>","error");
								}
								else
								{
									$("#instance_save").linkbutton("enable");
									$("#instance_save .l-btn-text").text("保存");
									$.messager.alert('错误',"<p>修改失败！</p>","error");
								}
	    					}
			    		},
						error: function() {
							$("#instance_save").linkbutton("enable");
							$("#instance_save .l-btn-text").text("保存");
							$.messager.alert('错误','<p>服务异常，修改异常错误码失败，请稍后重试或联系管理员!</p>', 'error');
						}
			    	});
				}
	 		}else{
	 			$.ajax({
		    		type: "get",
		    		url: BASE_URL_EXCEPTION_ERROR_CODE_SETTING_SEARCH + '/checkIsCanSave' + '?v=' + Math.random(),
		    		data: checkParam,
		    		contentType: "application/json",
		    		success: function(result) {
	    				if(result.isCanSave == true)
	    				{
	    					addField(newFieldInfo);
	    				}
	    				else 
	    				{
	    					if (result.errorCode == "DUPLICATE") 
	    					{
	        					$("#instance_save").linkbutton("enable");
	        					$("#instance_save .l-btn-text").text("保存");
	    						$.messager.alert('错误',"<p>新增失败，异常错误码已存在！</p>","error");
	    					}
	    					else
	    					{
	    						$("#instance_save").linkbutton("enable");
	    						$("#instance_save .l-btn-text").text("保存");
	    						$.messager.alert('错误',"<p>新增失败！</p>","error");
	    					}
	    				}
						
		    		},
					error: function() {
						$("#instance_save").linkbutton("enable");
						$("#instance_save .l-btn-text").text("保存");
						$.messager.alert('错误','<p>服务异常，新增异常错误码失败，请稍后重试或联系管理员!</p>', 'error');
					}
		    	});
	 		}
 		}
 	}
 	
 	function addField(newFieldInfo) {
 		if(!newFieldInfo)
 		{
 	 		var errorCode = $("#txtErrorCode").val();
 	 		var errorDesc = $("#txtErrorCodeDesc").val();
 	 		newFieldInfo = {errorCode:errorCode,errorDesc:errorDesc};
 		}
 		
 		var jsonData = JSON.stringify(newFieldInfo);
 		$.ajax({
			url: BASE_URL_EXCEPTION_ERROR_CODE_SETTING_CRUD  + '?v=' + Math.random(),
			type:'post',
			contentType:"application/json",
			data:jsonData,
			success:function(){
				$.messager.alert("提示","<p>新增成功!</p>","info");
				$('#win').window('close');
				$("#datagrid").datagrid("reload",BASE_URL_EXCEPTION_ERROR_CODE_SETTING_SEARCH + '/findByExample' + '?v=' + Math.random());
				$("#instance_save").linkbutton("enable");
				$("#instance_save .l-btn-text").text("保存");
			},
			error:function(){

				$.messager.alert("错误","<p>服务异常，新增异常错误码失败，请重新登录或稍后再试！</p>","error");	
				$("#instance_save").linkbutton("enable");
				$("#instance_save .l-btn-text").text("保存");
			}
		});
 	}
 	
 	function modifyErrorCode(newFieldInfo) {
 		var row = $('#datagrid').datagrid('getSelected');
 		if(!newFieldInfo)
 		{
 			var errorCode = $("#txtErrorCode").val();
 	 		var errorDesc = $("#txtErrorCodeDesc").val();
 	 		newFieldInfo = {errorCode:errorCode,errorDesc:errorDesc};
 		}
 		
 		var jsonData = JSON.stringify(newFieldInfo);
 		$.ajax({
			url: BASE_URL_EXCEPTION_ERROR_CODE_SETTING_CRUD + '/' + row.id  + '?v=' + Math.random(),
			type:'put',
			contentType:"application/json",
			data:jsonData,
			success:function(){
				$.messager.alert("提示","<p>修改成功!</p>","info");
				$('#win').window('close');
				$("#datagrid").datagrid("reload",BASE_URL_EXCEPTION_ERROR_CODE_SETTING_SEARCH + '/findByExample' + '?v=' + Math.random());
				$("#instance_save").linkbutton("enable");
				$("#instance_save .l-btn-text").text("保存");
			},
			error:function(){
				$.messager.alert("错误","<p>服务异常，修改失败，请重新登录或稍后再试！</p>","error");	
				$("#instance_save").linkbutton("enable");
				$("#instance_save .l-btn-text").text("保存");
			}
		});
 	}
 	//修改提示
 	function update(){
 		var row = $('#datagrid').datagrid('getSelected');
 		if(row == null){
 			$.messager.alert('警告',"<p>请选中要修改的记录!</p>","warning");
 		}else{
 			modifyOrAddFlag = false;
 			$('#addForm')[0].reset();
 			$("#txtErrorCode").val(row.errorCode);
			$("#txtErrorCodeDesc").val(row.errorDesc);
			$("#win").window('setTitle','异常错误码设置-修改');
			$(".easyui-validatebox").validatebox("validate");
			$('#win').window('open');
			
		}
 	}
	
 	//删除
 	function del(){
 		var row = $('#datagrid').datagrid('getSelected');
		if(row == null){
			$.messager.alert('警告',"<p>请选中要删除的记录!</p>","warning");
			return;
		}
 		var id = row.id;
 		var param = {id:id};
		$.messager.confirm('温馨提示','<p>你真的要删除么?</p>',function(r){
			if (r){
			 		$.ajax({
						url:BASE_URL_EXCEPTION_ERROR_CODE_SETTING_CRUD + '/' + row.id  + '?v=' + Math.random(),
						dataType:'json',
						type:'delete',
						contentType:"application/json",
						success:function(){
							$.messager.alert('提示','<p>删除成功!</p>','info');
							$("#datagrid").datagrid("reload", BASE_URL_EXCEPTION_ERROR_CODE_SETTING_SEARCH + '/findByExample?v=' + Date.parse(new Date()));
						},
						error:function(){
							$.messager.alert("错误","<p>服务异常，删除失败，请重新登录或稍后再试！</p>","error");	
						}
			 		});
			}
		});
	}
 	
 	
 	
 	
 	function onCancleEdit(){
 		$('#win').window('close');
 		$("#datagrid").datagrid("reload", BASE_URL_EXCEPTION_ERROR_CODE_SETTING_SEARCH + '/findByExample' + '?v=' + Math.random());
 	}

	function onSearch(){
		$("#datagrid").datagrid("reload", BASE_URL_EXCEPTION_ERROR_CODE_SETTING_SEARCH + '/findByExample' + '?v=' + Math.random());
	}
	
	function rePage(){
		$('#win').window('close');
		$("#datagrid").datagrid("reload", BASE_URL_EXCEPTION_ERROR_CODE_SETTING_SEARCH + '/findByExample' + '?v=' + Math.random());
	}